from SeerGlobal import *
from SeerDm import DmBase
import time
task = get_value('Task')
SeerDm = get_value('SeerDm')
dm = SeerDm.dm
hwnd = SeerDm.hwnd

"""
精灵 = ["小蘑菇","皮皮"]
task.addTask("BeiBao","0000",精灵)
"""

def inputID(_strID):
    """输入精灵ID"""
    for i in range(10):                       #删除键5次
        dm.KeyPressChar("back")              #删除键
                                             #以上内容为删除原有内容
    isb = dm.SendString(hwnd,_strID)         #输入精灵ID
    if(isb == 0):
        isb = dm.SendString2(hwnd,_strID)    #再次输入精灵ID
        if(isb == 0):
            for i in iter(_strID):           #再次输入精灵ID
                dm.KeyPressChar(i)
                time.sleep(0.01)
    time.sleep(0.5)
    dm.KeyPressChar("enter")

def mouseClick():
    """鼠标狂点"""
    dm.SetMouseDelay("windows",100)
    dm.LeftDoubleClick()
    dm.LeftDoubleClick()
    dm.SetMouseDelay("windows",10)

def 背包精灵数():
    """判断当前背包里总共存在几只精灵"""
    精灵数=9
    if(Fun("BeiBao","BeiBao_13") == 1):#1
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_14") == 1):#2
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_15") == 1):#3
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_16") == 1):#4
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_17") == 1):#5
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_18") == 1):#6
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_19") == 1):#7
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_20") == 1):#8
        精灵数=精灵数-1
    if(Fun("BeiBao","BeiBao_21") == 1):#9
        精灵数=精灵数-1
    return 精灵数
    
    
    
页=0
进度=0
def job(*args):
    """背包模块"""
    try:
        精灵组 = args[1]
    except:
        print('调用背包模块时缺少参数--精灵组')
        return
        #精灵组 = ["3512","3022","3463","3437","3107"]
    if len(精灵组) > 12:
        print("精灵组超出12只精灵,已自动退出。")
        task.stopTask("BeiBao")
    global 页
    global 进度
    task.setJobMaxInstances("BeiBao")       #设置同时最大允许执行数量
    if(Fun("BeiBao","BeiBao_1") == 1):      #背包页--左下黄色字(战)
        页=1
    if(Fun("BeiBao","BeiBao_5") == 1):      #仓库页--中上黄字(库)
        页=2
        if(Fun("BeiBao","BeiBao_26") == 1):    #仓库页--搜索结果分页--中上字(灵)下方赛尔小图
            页=3
    if(Fun("BeiBao","BeiBao_29") == 1):    #任何页--弹窗(xxx已经放入背包)
        Fun("BeiBao","BeiBao_30")          #点击确认按钮
        return                             #不把弹窗问题解决--不允许执行后面
    if(Fun("BeiBao","BeiBao_34") == 1):    #任何页--弹窗(xxx已经放入背包)
        Fun("BeiBao","BeiBao_35")          #点击确认按钮
        return                             #不把弹窗问题解决--不允许执行后面
    if(Fun("BeiBao","BeiBao_7") == 1):     #任何页--弹窗(没有找到符合的精灵)
        Fun("BeiBao","BeiBao_8")           #点击确认按钮
        return                             #不把弹窗问题解决--不允许执行后面
    
    
    if 页==2:
        Fun("BeiBao","BeiBao_6")            #仓库页--点击搜索框
        if len(精灵组) != 进度:
            _strID = 精灵组[进度]
            inputID(_strID)
            inputID(_strID)                 #重复一次避免BUG
            进度 = 进度 + 1
            if(Fun("BeiBao","BeiBao_7") == 1):  #任何页--弹窗(没有找到符合的精灵)
                Fun("BeiBao","BeiBao_8")        #点击确认按钮
                
            if(Fun("BeiBao","BeiBao_29") == 1):    #任何页--弹窗(xxx已经放入背包)
                Fun("BeiBao","BeiBao_30")          #点击确认按钮
            
            if(Fun("BeiBao","BeiBao_22") == 1): #仓库页--替换精灵页(一般不会出现)
                Fun("BeiBao","BeiBao_23")       #点击第一只精灵
                if(Fun("BeiBao","BeiBao_24") == 1): #仓库页--替换精灵页--确认替换按钮
                    Fun("BeiBao","BeiBao_25")       #点击确认替换按钮
        else:
            print("已经完成！")
            #Fun("BeiBao","BeiBao_10")           #仓库页--点击右上叉号(关闭)
            Fun("BeiBao","BeiBao_11")           #仓库页--点击右上叉号(关闭)
            mouseClick()
    if 页==3:
        Fun("BeiBao","BeiBao_9")            #仓库页--搜索结果分页--点击放入背包按钮
        if(Fun("BeiBao","BeiBao_27") == 1):    #仓库页--搜索结果分页--左上问号图标(点击放入背包后才出现)
            pass
        if(Fun("BeiBao","BeiBao_28") == 1):    #仓库页--搜索结果分页--下方放入背包灰色图标
            Fun("BeiBao","BeiBao_31")          #点击搜索结果的叉号(关闭)
            mouseClick()
        
    if 页==1:
        x = Fun("BeiBao","BeiBao_2")        #背包页--第一格小图标
        y = Fun("BeiBao","BeiBao_3")        #背包页--第七格小图标
        if x==y==1:                         #说明此时背包为空
            Fun("BeiBao","BeiBao_12")       #点击右下精灵仓库按钮
        else:
            if len(精灵组) != 进度:
                Fun("BeiBao","BeiBao_4")        #点击入库按钮
                if(Fun("BeiBao","BeiBao_32") == 1):#任何页--弹窗(数据非法)
                    Fun("BeiBao","BeiBao_33")      #点击确认按钮
            else:
                精灵数 = 背包精灵数()
                print("精灵数:" + str(精灵数))
                if len(精灵组) > 精灵数:
                    print("精灵数量小于给定数量，重启该任务")
                    print('精灵组：' + str(len(精灵组)))
                    print('精灵数：' + str(精灵数))
                    task.stopTask("BeiBao")
                    task.addTask("BeiBao","BeiBao",精灵组)
                else:
                    task.stopTask("BeiBao")
            